import { C_CustomsReward } from "../../C_Config";
import C_SwitchFrame from "../../component/C_SwitchFrame";
import C_App from "../../core/C_App";
import { C_NotificationConst } from "../../core/C_NotificationConst";
import BaseView from "./C_BaseView";

const { ccclass, property } = cc._decorator;
/** 选关界面 */
@ccclass
export default class C_CustomsView extends BaseView {
    @property(cc.Label)
    indexLab: cc.Label = null;

    @property(cc.Node)
    startNode: cc.Node = null;

    @property(cc.Node)
    grayNode: cc.Node = null;

    @property(cc.Node)
    preNode: cc.Node = null;

    @property(cc.Node)
    nextNode: cc.Node = null;

    @property(cc.Label)
    rewardLab: cc.Label = null;

    @property(C_SwitchFrame)
    stateFrame: C_SwitchFrame = null;

    maxIndex: number;
    curIndex: number;
    setCurIndex(index: number) {
        this.curIndex = index;
        this.updateView();
    }

    initView(index: number) {
        this.maxIndex = this.curIndex = index;
        this.updateView();
    }

    onTouchEnd() {
        if (this.curIndex <= this.maxIndex)
            C_App.NotificationCenter.dispatch(C_NotificationConst.ENTER_CUSTOMS, this.curIndex)
    }

    onTouchPre() {
        if (this.curIndex > 0) {
            this.curIndex--;
            this.updateView()
        }
    }

    onTouchNext() {
        if (this.curIndex < 50) {
            this.curIndex++;
            this.updateView();
        }
    }

    updateView() {
        if (this.curIndex == this.maxIndex) {
            this.stateFrame.node.active = false;
        }
        else {
            this.stateFrame.node.active = true;
            if (this.curIndex > this.maxIndex) {
                this.stateFrame.frameIndex = 0
            }
            else {
                this.stateFrame.frameIndex = 1;
            }
        }
        this.indexLab.string = `第${this.curIndex + 1}关`;
        if (this.curIndex <= this.maxIndex) {
            this.grayNode.active = false;
            this.startNode.active = true;
        }
        else {
            this.grayNode.active = true;
            this.startNode.active = false;
        }
        this.preNode.active = this.curIndex != 0;
        this.nextNode.active = this.curIndex != 50;

        const num = this.getReward();
        this.rewardLab.string = 'X' + num;
    }

    getReward() {
        for (let key in C_CustomsReward) {
            if (parseInt(key) > this.curIndex) {
                return C_CustomsReward[key];
            }
        }
        return 0;
    }

    onTouchClose() {
        cc.systemEvent.emit("C_GameClose")
        CC_DEBUG && console.log("退出游戏");
    }
}
